<!-- 前端页面代码 -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>修改客户信息</title>
    <!-- 引入thymeleaf模板 -->
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <h1>修改客户信息</h1>
    <form id="customerForm" method="post" enctype="multipart/form-data">
        <input type="hidden" id="id" name="id" th:value="${customer.id}">
        <div class="form-group">
            <label for="name">客户姓名</label>
            <input type="text" class="form-control" id="name" name="name" th:value="${customer.name}">
        </div>
        <div class="form-group">
            <label for="gender">性别</label>
            <select class="form-control" id="gender" name="gender">
                <option value="true" th:selected="${customer.gender == '男'}">男</option>
                <option value="false" th:selected="${customer.gender == '女'}">女</option>
            </select>
        </div>
        <div class="form-group">
            <label for="age">年龄</label>
            <input type="number" class="form-control" id="age" name="age" th:value="${customer.age}">
        </div>
        <div class="form-group">
            <label for="height">身高</label>
            <input type="number" class="form-control" id="height" name="height" th:value="${customer.height}">
        </div>
        <div class="form-group">
            <label for="weight">体重</label>
            <input type="number" class="form-control" id="weight" name="weight" th:value="${customer.weight}">
        </div>
        <div class="form-group">
            <label for="income">收入（万/元）</label>
            <input type="number" class="form-control" id="income" name="income" th:value="${customer.income}">
        </div>
        <div class="form-group">
            <label for="hasCar">是否有车</label>
            <select class="form-control" id="hasCar" name="hasCar">
                <option value="true" th:selected="${customer.hasCar}">是</option>
                <option value="false" th:selected="${!customer.hasCar}">否</option>
            </select>
        </div>
        <div class="form-group">
            <label for="hasHouse">是否有房</label>
            <select class="form-control" id="hasHouse" name="hasHouse">
                <option value="true" th:selected="${customer.hasHouse}">是</option>
                <option value="false" th:selected="${!customer.hasHouse}">否</option>
            </select>
        </div>
        <div class="form-group">
            <label for="isDivorced">是否离异 0表示未婚，1表示已离异，2表示丧偶</label>
            <input type="text" class="form-control" id="isDivorced" name="isDivorced" th:value="${customer.isDivorced}">
        </div>
        <div class="form-group">
            <label for="nation">民族</label>
            <input type="text" class="form-control" id="nation" name="nation" th:value="${customer.nation}">
        </div>
        <div class="form-group">
            <label for="education">学历</label>
            <input type="text" class="form-control" id="education" name="education" th:value="${customer.education}">
        </div>
        <div class="form-group">
            <label for="occupation">职业</label>
            <input type="text" class="form-control" id="occupation" name="occupation" th:value="${customer.occupation}">
        </div>
        <div class="form-group">
            <label for="baseAddress">用户户籍地址</label>
            <input type="text" class="form-control" id="baseAddress" name="baseAddress" th:value="${customer.baseAddress}">
        </div>
        <div class="form-group">
            <label for="currentAddress">现居住地</label>
            <input type="text" class="form-control" id="currentAddress" name="currentAddress" th:value="${customer.currentAddress}">
        </div>
        <div class="form-group">
            <label for="personality">性格</label>
            <input type="text" class="form-control" id="personality" name="personality" th:value="${customer.personality}">
        </div>
        <div class="form-group">
            <label for="friendEvaluation">朋友评价</label>
            <input type="text" class="form-control" id="friendEvaluation" name="friendEvaluation" th:value="${customer.friendEvaluation}">
        </div>
        <div class="form-group">
            <label for="idealType">理想型</label>
            <input type="text" class="form-control" id="idealType" name="idealType" th:value="${customer.idealType}">
        </div>
        <button type="button" class="btn btn-primary" id="submitBtn">修改</button>
        <button type="button" class="btn btn-secondary" id="backBtn">返回</button>
    </form>

</div>
<script>
    $(function() {

        var uuid = "[[${userUUid}]]"
        console.log(uuid);
        // 点击修改按钮
        $('#submitBtn').click(function () {
            // 获取表单数据
            var formData = new FormData($('#customerForm')[0]);
            // 判断是否有变化
            if (formData.get('name') == '[[${customer.name}]]' &&
                formData.get('gender') == '[[${customer.gender}]]' &&
                formData.get('age') == '[[${customer.age}]]' &&
                formData.get('height') == '[[${customer.height}]]' &&
                formData.get('weight') == '[[${customer.weight}]]' &&
                formData.get('income') == '[[${customer.income}]]' &&
                formData.get('hasCar') == '[[${customer.hasCar}]]' &&
                formData.get('hasHouse') == '[[${customer.hasHouse}]]' &&
                formData.get('isDivorced') == '[[${customer.isDivorced}]]' &&
                formData.get('nation') == '[[${customer.nation}]]' &&
                formData.get('education') == '[[${customer.education}]]' &&
                formData.get('occupation') == '[[${customer.occupation}]]' &&
                formData.get('baseAddress') == '[[${customer.baseAddress}]]' &&
                formData.get('currentAddress') == '[[${customer.currentAddress}]]' &&
                formData.get('personality') == '[[${customer.personality}]]' &&
                formData.get('friendEvaluation') == '[[${customer.friendEvaluation}]]' &&
                formData.get('idealType') == '[[${customer.idealType}]]') {
                alert('没有修改任何信息！');
                return;
            }
            // 判断不能为空
            if (formData.get('name') == '' ||
                formData.get('gender') == '' ||
                formData.get('age') == '' ||
                formData.get('height') == '' ||
                formData.get('weight') == '' ||
                formData.get('income') == '' ||
                formData.get('hasCar') == '' ||
                formData.get('hasHouse') == '' ||
                formData.get('isDivorced') == '' ||
                formData.get('nation') == '' ||
                formData.get('education') == '' ||
                formData.get('occupation') == '' ||
                formData.get('baseAddress') == '' ||
                formData.get('currentAddress') == '' ||
                formData.get('personality') == '' ||
                formData.get('friendEvaluation') == '' ||
                formData.get('idealType') == '') {
                alert('所有字段都不能为空！');
                return;
            };
            var jsonObject = {};
            formData.forEach(function(value, key){
                jsonObject[key] = value;
            });
            // 发送修改请求
            $.ajax({
                url: '/tempUser/update',
                type: 'post',
                data: JSON.stringify(jsonObject),
                contentType: 'application/json',
                success: function (result) {
                    if (result.code == 200) {
                        alert('修改成功！');
                    } else {
                        alert('修改失败：' + result.msg);
                    }
                },
                error: function () {
                    alert('修改失败！');
                }
            });
        });
        // 点击返回按钮
        $('#backBtn').click(function() {
            var fileId = "[[${fileId}]]"
            console.log("fileId:" + fileId);
            window.location.href = '/uploadUser/' + fileId;
        })
    });


</script>
</body>
</html>